{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>区/县{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable layui-row">
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">区/县名称</label>
				<div class="layui-input-inline layui-form">
					<input class="layui-input" name="key" id="key" placeholder="请输入区/县名称">
				</div>
			</div>
        </div>
		<form class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">所属省</label>
				<div class="layui-input-inline layui-form">
					<select id="pid" name="" lay-filter="province">
						<option value="">请选择省</option>
						{volist name="province" id="province"}
							<option value="{$province.id}">{$province.name}</option>
						{/volist}
					</select>
				</div>
			</div>	
		</form>	
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">所属市</label>
				<div class="layui-input-inline layui-form">
					<div class="layui-input-inline">
						<select id="pid1">
							<option value="">请选择市</option>
							
						</select>
					</div>
				</div>
			</div>	
		</div>
		<div class="layui-form layui-form-pane" style="float:left">
			<div class="layui-form-item">
				<label class="layui-form-label">{:lang('status')}</label>
				<div class="layui-input-inline layui-form">
					<select id="opens">
						<option value=""></option>
						{volist name="open" id="ope"}
						<option value="{$key}">{$ope}</option>
						{/volist}
					</select>
				</div>
			</div>		
		</div>
    </div>
	<div class="demoTable layui-row">
		<button class="layui-btn" id="search" data-type="reload">{:lang('search')}</button>
        <a href="{:url('index')}" class="layui-btn">{:lang('reset')}</a>
	</div>	
	<blockquote class="layui-elem-quote">
        <a href="{:url('add')}" class="layui-btn layui-btn-small">添加市级</a>
    </blockquote>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="status">
	<input type="checkbox" name="status" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('enabled')}|{:lang('disabled')}" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="action">
	 <a href="{:url('edit')}?id={{d.id}}" class="layui-btn layui-btn-xs">{:lang('edit')}</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">{:lang('del')}</a>
</script>
{include file="common/foot"/}
<script>
     layui.use(['table','form'], function() {
        var table = layui.table,form = layui.form,$ = layui.jquery;
        var tableIn = table.render({
            id: 'ad',
            elem: '#list',
            url: '{:url("index")}',
            method: 'post',
            page: true,
            cols: [[
                {field: 'id',sort: true,align: 'center', title: '{:lang("id")}', width: 80, fixed: true},
				{field: 'pid1',align: 'center', title: '所属省', width: 200},
				{field: 'pid',align: 'center', title: '所属市', width: 200},
                {field: 'name',align: 'center', title: '区县名称', width: 300},
				{field: 'status', align: 'center', title: '{:lang("status")}', width: 100, toolbar: '#status'},
                {width: 200, align: 'center',title: '{:lang("action")}', toolbar: '#action'}
            ]],
            limit: '' //每页默认显示的数量
        });
        //搜索
        $('#search').on('click', function(){
			//name
            var key = $('#key').val();
			
			//省
			var pid = $('#pid').val();
			//市
			var pid1 = $('#pid1').val();
			var status = $('#opens').val();
            if($.trim(key)==='' && $.trim(pid)==='' && $.trim(pid1)==='' && $.trim(status)==='') {
                layer.msg('{:lang("query_condition")}',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key:key,pid:pid,pid1:pid1,status:status}
            });
        });
		form.on('switch(status)', function(obj){
			
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
			
            var status = obj.elem.checked===true?1:0;
            $.post('{:url("editState")}',{'id':id,'status':status},function (res) {
                layer.close(loading);
                if (res.errorCode==8888){
						layer.msg(res.msg,{time:1000,icon:1});
						//tableIn.reload();
                    }else{
                        layer.msg(res.msg,{time:1000,icon:2},function(){
							location.href = res.url;
							});
                        return false;
                    }

            })
        });	
        table.on('tool(list)', function(obj) {
            var data = obj.data;
            if (obj.event === 'del') {
				//单个删除方法
                layer.confirm('确定要删除该区县吗？', function(index){
                    var loading = layer.load(1, {shade: [0.1, '#fff']});
                    $.post("{:url('del')}",{id:data.id},function(res){
                        layer.close(loading);
                        if(res.errorCode == 8888){
                            layer.msg(res.msg,{time:1000,icon:1});
                            obj.del();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                });
            }
        });
    });	
	
	layui.use(['form', 'layer'], function () {
            var form = layui.form, layer = layui.layer,$= layui.jquery;
            form.on('select(province)', function(data){
                var pid = data.value;
                //var loading = layer.load(1, {shade: [0.1, '#fff']});
                $.get("{:url('getRegion')}?pid=" + pid, function (data) {
                    //layer.close(loading);
					//console.log(data);
					var html='<option value="">请选择市</option>';
                    $.each(data, function (i, value) {
                        html += '<option value="number:'+value.id+'">'+value.name+'</option>';
					});
                $('#pid1').html(html);
                form.render()
			});
        });
            
    });	
	
	
</script>
</body>
</html>